﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.Percent
{
    public class PercentController
    {
        public static DataTable GetConfig()
        {
            PercentFilter filter = new PercentFilter();
            return filter.GetConfigPercent();           
        }

        public static DataTable GetConfigIDByPercent(decimal percet)
        {
            PercentFilter filter = new PercentFilter();
            filter.PercentConfig = percet;
            return filter.GetConfigPercent();
        }
        
        public static Percent GetConfigByID(int ID)
        {
            PercentFilter filter = new PercentFilter();
            filter.ConfigID = ID;
            DataTable dt = filter.GetConfigPercent();
            if (ValidationHelper.HasRow(dt))
            {
                return new Percent(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public static bool Insert(Percent percet)
        {
            return ExecuteStoredProcedure(percet, StoreProceduAction.AddNew.ToString());
        }

        public static bool Delete(Percent percet)
        {
            return ExecuteStoredProcedure(percet, StoreProceduAction.Delete.ToString());
        }

        public static bool UpdateValue(Percent percet, StoreProceduAction action)
        {
            return ExecuteStoredProcedure(percet, action.ToString());
        }
        

        private static bool ExecuteStoredProcedure(Percent percet, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "PercentConfig";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@ConfigID", percet.ConfigID);
            sqlHelper.AddParameter("@NumOfPeople", percet.NumOfPeople);
            sqlHelper.AddParameter("@percent", percet.PercentConfig);
            return sqlHelper.ExecuteSQL(sProc);
        }
    }
}
